/**
 * Abbreviation
 * library for common country and language codes
 * @author kzm
 * @version 1.0
 * @license MIT-License (2007)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 
 */
package com.mediacatalyst.core
{

	public class Abbreviation{
		private static var _defined:Array;
		private static var _default:Abbreviation;
		private var _language:String;
		private var _country:String;
		private var _name:String;
		/**
		 * constructor
		 * @throws error if the codes are illegal
		 * @param name full name of the collection, i.e. Great Britian
		 * @param country country code, i.e. gb
		 * @param language language code, i.e. en
		 * @see country
		 * @see language
		 */
		public function Abbreviation(name:String,country:String=null,language:String=null){
			this.name = name;
			this.country = country;
			this.language = language;
			if(_defined == null)
				_defined = new Array();
			_defined.push(this);
		}
		/**
		 * set country code
		 * @throws error if abbr is longer than two characters
		 * @param abbr the code
		 */
		public function set country(abbr:String):void{
			if(abbr.length>2)
				throw new Error("Abbreviation: given country code is too long.");
			_country = abbr;
		} 
		/**
		 * returns the country code
		 * @return the country code
		 */
		public function get country():String{
			return _country;
		} 
		/**
		 * set the name
		 * @param name the full name
		 */
		public function set name(name:String):void{
			_name = name;
		} 
		/**
		 * returns the full name
		 * @return the name
		 */
		public function get name():String{
			return _name;
		} 
		/**
		 * set language code
		 * @throws error if abbr is longer than two characters
		 * @param abbr the code
		 */
		public function set language(abbr:String):void{
			if(abbr.length>2)
				throw new Error("Abbreviation: given language code is too long.");
			_language = abbr;
		} 
		/**
		 * returns the language code
		 * @return the language code
		 */
		public function get language():String{
			return _language;
		} 
		/**
		 * gets the collection by name
		 * @param name the name of the collection
		 * @return returns an array of all found entries
		 */
		public static function getByName(name:String):Array{
			return getByAttribute(name,"name");
		}
		/**
		 * gets the collection by language code
		 * @throws error if abbr is longer than two characters
		 * @param abbr the language code
		 * @return returns an array of all found entries
		 */
		public static function getByLanguage(abbr:String):Array{
			if(abbr.length>2)
				throw new Error("Abbreviation: given language code is too long.");
			return getByAttribute(abbr,"language");
		}
		/**
		 * gets the collection by country code
		 * @throws error if abbr is longer than two characters
		 * @param abbr the country code
		 * @return returns an array of all found entries
		 */
		public static function getByCountry(abbr:String):Array{
			if(abbr.length>2)
				throw new Error("Abbreviation: given country code is too long.");
			return getByAttribute(abbr,"country");
		}
		/**
		 * gets the collection by given field
		 * @param abbr the key
		 * @param attr the field name
		 * @return returns an array of all found entries
		 */
		protected static function getByAttribute(abbr:String,attr:String):Array{
			var rtn:Array = new Array();
			for each(var abb:Abbreviation in _defined){
				if(abb[attr] == abbr)
					rtn.push(abb);
			}
			return rtn;
		}
		/**
		 * get the default collection
		 * @return the default collection
		 */
		public static function get normal():Abbreviation{
			return _default;
		}
		/**
		 * set the default collection
		 * @param abb the collection to be set default
		 */
		public static function set normal(abb:Abbreviation):void{
			_default = abb;
		}
		/**
		 * returns the string representation of the object
		 * @return the string
		 */
		public function toString():String{
			return "Abbreviation(name="+name+",country="+country+",language="+language+")";
		}
	}
}